<template>
  <a-modal
    :title="`黑名单${model&&model.id>0?'编辑':'新增'}`"
    :width="640"
    :visible="visible"
    :confirmLoading="loading"
    @ok="() => { $emit('ok') }"
    @cancel="() => { $emit('cancel') }"
  >
    <a-spin :spinning="loading">
      <a-form :form="form" :labelCol="{sm: {span: 4}}" :wrapperCol="{sm: {span: 20}}" :selfUpdate="true">
        <!-- 检查是否有 id 并且大于0，大于0是修改。其他是新增，新增不显示主键ID -->
        <a-form-item v-show="model && model.id > 0" label="ID">
          <a-input v-decorator="['id', { initialValue: 0 }]" disabled />
        </a-form-item>
        
        <a-form-item label="类型">
          <a-select v-decorator="['cType', {rules: [{ required: true, message: '内容类型不能为空' }]}]" placeholder="请选择拉黑类型">
              <a-select-option v-for="it in typeArr" :key="it.id" :value="Number(it.id)">{{it.text}}</a-select-option>
            </a-select>
        </a-form-item>
        <a-form-item label="内容">
          <a-input v-decorator="['content', {rules: [{ required: true, message: '内容不能为空' }]}]"  placeholder="请输入拉黑内容"/>
        </a-form-item>
        <a-form-item label="截至时间">
          <a-date-picker
            show-time
            v-decorator="[
              'upToTime',
              {rules: [{ required: true, message: '请输入拉黑截至时间' }]}
            ]"
            format="YYYY-MM-DD HH:mm:ss"
          />
        </a-form-item>
        <a-form-item label="拉黑原因">
          <a-textarea v-decorator="['remark']" :rows="4" placeholder="请输入拉黑原因"/>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'

// 表单字段
const fields = ['remark', 'content','cType','upToTime','id']

export default {
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    loading: {
      type: Boolean,
      default: () => false
    },
    typeArr: {
      type: Array,
      default: () => []
    },
    model: {
      type: Object,
      default: () => null
    }
  },
  data () {
    return {
      form: this.$form.createForm(this)
    }
  },
  created () {
    // 防止表单未注册
    fields.forEach(v => this.form.getFieldDecorator(v))

    // 当 model 发生改变时，为表单设置值
    this.$watch('model', () => {
      this.model && this.form.setFieldsValue(pick(this.model, fields))
    })
  },
  methods:{

  }
}
</script>
